<?php
namespace Album\Controller;
use Think\Controller;

/**
 * Class AdminAlbumImgController
 * @package Scrollnav\Controller
 * 列表相册图库
 */
class AdminAlbumImgController extends Controller {

    private $model_name = 'AlbumImg';

    public function index(){
        $data = filterInput($_GET);
        $model = D($this->model_name);
        $list = $model->queryField('albumid', $data['pid']);
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        $model = D($this->model_name);
        if(IS_POST){
            $data = filterInput($_POST);
            $model->addObj($data);
            $this->success(LANG('save_success'));
        }else if(IS_GET){
            $this->display();
        }
    }

    public function edit(){
        $model = D($this->model_name);
        if(IS_POST){
            $data = filterInput($_POST);
            $model->save($data);
            $this->success(LANG('save_success'));
        }else if(IS_GET){
            $data = filterInput($_GET);
            $id = $data['id'];
            $obj = $model->queryObj($id, 'id');
            $this->assign('obj', $obj);
            $this->display();
        }
    }

    public function del(){
        $model = D($this->model_name);
        $data = filterInput($_GET);
        $id = $data['id'];
        $model->delete($id);
    }

    public function uploadify(){
        if (!empty($_FILES)) {
            import("@.Think.UploadFile");
            $upload = new \Think\Upload();
            $upload->rootPath  = C('UPLOAD_ROOT_PATH');
            $upload->savePath = C('UPLOAD_SAVE_PATH');
            $upload->autoSub = false;
            $upload->saveRule = uniqid; //上传规则
            $info = $upload->upload();
            if(!$info){
                echo $this->error($upload->getError());//获取失败信息
            } else {
                //成功
                $fileArray = "";
                foreach ($info as $file) {
                    $fileArray = '/upload/' . $file['savepath'] . $file['savename'];
                }
                echo trim($fileArray);
            }
        }
    }
}